/*
** parseur_xml.c for  in /home/lucas/projet/philosophe/laidfilauxzauphe
** 
** Made by Lucas Stefas
** Login   <stefas_l@epitech.net>
** 
** Started on  Sat Apr 30 10:06:06 2011 Lucas Stefas
** Last update Sat Apr 30 10:08:26 2011 Lucas Stefas
*/

#define _GNU_SOURCE

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "plsp.h"



int	findendbalise(char *balise, char *name, int namelen)
{
  if (balise[namelen] == '>' && balise[0] == '<'
      && balise[1] == '/')
    {
      balise[namelen] = '\0';
      if (my_strcmp(balise + 2, name))
	return (1);
    }
  return (0);
}

lifo	*getaline(char *str, lifo *list, size_t *res)
{
  int	i = 0;
  int	ii = 0;
  int	iii = 0;

  if ((ii = findouverture(str, &i)) != -1)
    list = addinlist(list, getbalisename(str + i, ii));
  if ((iii = findfermeture(str, &i)) != -1)
    {
      if (!findendbalise(str + iii, list->str, i - iii))
      {
	fprintf(stderr, "balise de fermeture `%s'", list->str); 
	fprintf(stderr, " mal positionner\n");
	exit(EXIT_FAILURE);
      }
      list = popelem(list);
    }
  if (iii - ii != 1)
    {
      str[iii] = '\0';
      *res = atoi(str + ii + 1);
    }
  return (list);
}

void	printmyerror(int a, int b)
{
  if (a)
    {
      fprintf(stderr, "le nombre de baguette (%d) et", a);

      fprintf(stderr, "/ou le nombre de philosophe (%d) ", b);
      fprintf(stderr, "est trop faible, les valeur par ");
      fprintf(stderr, "defaut seront utiliser\n");
    }
  else
    {
      fprintf(stderr, "il semblerais qu'une valeur ai ");
      fprintf(stderr, "etais entree a l'exterrieur des balise\n");
      exit(EXIT_FAILURE);
    }
}

lifo	*readfile(FILE *fichier, lifo *list, nbtruc *truc)
{
  char		*line = NULL;
  size_t	i;

  while (getline(&line, &i, fichier) != -1)
    {
      i = 0;
      line = epurstr(line);
      list = getaline(line, list, &i);
      if (!truc->nbbaguette && i)
	truc->nbbaguette = i;
      else if (truc->nbbaguette && !truc->nbphilosophe && i)
	truc->nbphilosophe = i;
      else if (truc->nbphilosophe && i)
	printmyerror(0, 0);
    }
  if (truc->nbbaguette <= 1 || truc->nbphilosophe < 1)
    {
      printmyerror((int)truc->nbbaguette, (int)truc->nbphilosophe);
      truc->nbbaguette = 7;
      truc->nbphilosophe = 7;
    }
  printf("%d %d\n", (int)truc->nbbaguette, (int)truc->nbphilosophe);
  return (list);
}

nbtruc	parseur(char *str)
{
  FILE		*fichier;
  lifo		*list;
  nbtruc	truc;

  truc.nbbaguette = 0;
  truc.nbphilosophe = 0;
  if ((list = malloc(sizeof(lifo))) == NULL)
    {
      fprintf(stderr, "Error: %s\n", strerror(errno));
      exit(EXIT_FAILURE);
    }
  list->str = NULL;
  list->next = NULL;
  if ((fichier = fopen(str, "r")) == NULL)
    {
      fprintf(stderr, "Error: %s\n", strerror(errno));
      exit(EXIT_FAILURE);
    }
  list = readfile(fichier, list, &truc);
  if (list)
    {
      fprintf(stderr, "il manque des fermeture de balise\n");
      exit(EXIT_FAILURE);
    }
  return (truc);
}
